<?php

namespace App\Models\UserModels;

use App\Models\ParentModel;
use DB;

/**
 * 用户监控model
 * @author puppet
 */
class Userinfo extends ParentModel
{
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'pt_user_info';

    /**
     * DB::直接操作的数据库表名
     * @var string
     */
    protected $db_table = 'pt_user_info';

    /**
     * 主键 默认id。
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 指定是否模型应该被戳记时间。
     *
     * @var bool
     */
    public $timestamps = false;


    /**
     *    获取用户监控数据
     *    $type 0(主页用户)  1(单视频用户)   $page 第几页   $role_id角色id   partner_id登录的ID
     */
    function selectData($parames)
    {
        $values = [1];
        $size = (isset($params['limit']) && intval($params['limit']) > 0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
        $page = isset($parames['page']) ? intval($parames['page']) : 1;
        $offset = ($page - 1) * $size;

        if (isset($parames['type']) && $parames['type'] == 2) {
            $values[0] = 2;
        }
        if (isset($parames['type']) && $parames['type'] == 3) {
            $values[0] = 3;
        }
        $countSql = "select count(DISTINCT number_id) as count from $this->table where info_type = ?";
        $sql = "select id,partner_id,info_id,info_name,info_img,info_signature,info_follow,info_fans,info_support,info_opus,info_vurl,info_date,number_id";
        $sql .= " from $this->table where info_type = ? ";

        if (isset($parames['name']) && !empty($parames['name'])) {
            $countSql .= " and info_name like '%{$parames['name']}%'";
            $sql .= " and info_name like '%{$parames['name']}%'";
        }
        $sql .= " group by number_id order by info_date desc limit $offset,$size";
        // echo $sql;exit;
        $count = DB::select($countSql, $values);
        $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
        // print_r($sql);exit();
        $data = DB::select($sql, $values);
        if (!empty($data) && $parames['type'] == 1) {//主页
            $i = 0;
            foreach ($data as $k => $v) {
                ++$i;
                $name=DB::select("select user_name from pt_partner where  id={$v['partner_id']}");
                $data[$k]['fname'] =isset($name[0]['user_name']) ? $name[0]['user_name']:'暂无';
                $data[$k]['kid'] = $i;
                $data[$k]['info_signature'] = "<div style='width:200px;height:100px;overflow:auto;'>{$data[$k]['info_signature']}<div>";
                $data[$k]['img_url'] = "<a href=\"https://www.douyin.com/user/" . str_replace('sec_uid=', '', $v['number_id']) . "\" target='_blank'><img src='{$v['info_img']}' width='100' height='100'></a>";

                $num_fls = $num_sop = "";
                //关注/粉丝数
                $num_fls .= "<span style='color:red;font-size:18px;'>" . $v['info_follow'] . "</span> 关注<br/><br/>";
                $num_fls .= "<span style='color:red;font-size:18px;'>" . ($v['info_fans'] >= 10000 ? round($v['info_fans'] / 10000, 1) . " W" : $v['info_fans']) . "</span> 粉丝";
                $data[$k]['num_fls'] = $num_fls;

                //作品/获赞数
                $num_sop .= "<span style='color:red;font-size:18px;'>" . $v['info_opus'] . "</span> 作品<br/><br/>";
                $num_sop .= "<span style='color:red;font-size:18px;'>" . ($v['info_support'] >= 10000 ? round($v['info_support'] / 10000, 1) . " W" : $v['info_support']) . "</span> 赞";
                $data[$k]['num_sop'] = $num_sop;
                $Ccount=DB::select("select count(id) as count from pt_user_info_comment where type=?  and pt_type=1",[$v['id']]);
                $Ccount=isset($Ccount[0]['count']) ? $Ccount[0]['count'] : 0;
                $staTime=date('Y-m-d',strtotime(date('Y-m-d',time()-24*3600)))." 22:30:00";
                $Ncount=DB::select("select count(id) as count from pt_user_info_comment where type=? and pt_type=1 and update_time>='{$staTime}'",[$v['id']]);
                $Ncount=isset($Ncount[0]['count']) ? $Ncount[0]['count'] : 0;
                $data[$k]['allCount']="{$Ccount} / {$Ncount}";
                $title="账户昵称：".$v['info_name'];
                $data[$k]['operation']="<a href=\"javascript:;\" title=\"查看评论\"><span class=\"glyphicon glyphicon-eye-open\" onclick=\"showContent({$v['id']},'{$title}')\"></span></a>";
            }
        } elseif (!empty($data) && $parames['type'] == 2) {//视频
            $i = 0;
            foreach ($data as $k => $v) {
                ++$i;
                $name=DB::select("select user_name from pt_partner where  id={$v['partner_id']}");
                $data[$k]['fname'] =isset($name[0]['user_name']) ? $name[0]['user_name']:'暂无';
                $data[$k]['kid'] = $i;//info_support  round
                $data[$k]['info_signature'] = "<div style='width:200px;height:100px;overflow:auto;'>{$data[$k]['info_signature']}<div>";
                $data[$k]['img_url'] = "<a href='https://www.douyin.com/video/{$v['number_id']}' target='_blank'><img src='{$v['info_img']}' width='70' height='100'></a>";

                $num_fls = $num_sop = $num_fans = "";
                //评论数
                if ($v['info_opus'] >= 10000) {
                    $num_fls = "<span style='color:red;font-size:18px;'>" . round($v['info_opus'] / 10000, 1) . " W</span> 评论";
                } else {
                    $num_fls = "<span style='color:red;font-size:15px;'>" . $v['info_opus'] . "</span> 评论";
                }
                $data[$k]['num_fls'] = $num_fls;

                //获赞数
                if ($v['info_support'] >= 10000) {
                    $num_sop = "<span style='color:red;font-size:18px;'>" . round($v['info_support'] / 10000, 1) . " W</span> 赞";
                } else {
                    $num_sop = "<span style='color:red;font-size:15px;'>" . $v['info_support'] . "</span> 赞";
                }
                $data[$k]['num_sop'] = $num_sop;

                //分享数
                if ($v['info_fans'] >= 10000) {
                    $num_fans .= "<span style='color:red;font-size:18px;'>" . round($v['info_fans'] / 10000, 1) . " W</span> 次分享";
                } else {
                    $num_fans .= "<span style='color:red;font-size:15px;'>" . $v['info_fans'] . "</span> 次分享";
                }
                //评论数
                $Ccount=DB::select("select count(id) as count from pt_user_info_comment where type=? and pt_type=1 and video_id='{$v['number_id']}'",[0]);
                $data[$k]['allCount']=isset($Ccount[0]['count']) ? $Ccount[0]['count'] : 0;
                $data[$k]['num_fans'] = $num_fans;
                $title="视频标题：".$v['info_signature'];
                $data[$k]['operation']="<a href=\"javascript:;\" title=\"查看评论\"><span class=\"glyphicon glyphicon-eye-open\" onclick=\"showContent('{$v['number_id']}','{$title}')\"></span></a>";
            }
        } elseif (!empty($data) && $parames['type'] == 3) {//视频
            $i = 0;
            foreach ($data as $k => $v) {
                ++$i;
                $name=DB::select("select user_name from pt_partner where id={$v['partner_id']}");
                $data[$k]['fname'] =isset($name[0]['user_name']) ? $name[0]['user_name']:'暂无';
                $data[$k]['kid'] = $i;//info_support  round
                $data[$k]['info_signature'] = "<div style='width:200px;height:100px;overflow:auto;'>{$data[$k]['info_signature']}<div>";
                $data[$k]['img_url'] = "<a href='https://www.douyin.com/video/{$v['number_id']}' target='_blank'><img src='{$v['info_img']}' width='70' height='100'></a>";

                $num_fls = $num_sop = $num_fans = "";
                //评论数
                if ($v['info_opus'] >= 10000) {
                    $num_fls = "<span style='color:red;font-size:18px;'>" . round($v['info_opus'] / 10000, 1) . " W</span> 评论";
                } else {
                    $num_fls = "<span style='color:red;font-size:15px;'>" . $v['info_opus'] . "</span> 评论";
                }
                $data[$k]['num_fls'] = $num_fls;

                //获赞数
                if ($v['info_support'] >= 10000) {
                    $num_sop = "<span style='color:red;font-size:18px;'>" . round($v['info_support'] / 10000, 1) . " W</span> 赞";
                } else {
                    $num_sop = "<span style='color:red;font-size:15px;'>" . $v['info_support'] . "</span> 赞";
                }
                $data[$k]['num_sop'] = $num_sop;
                //评论数
                $Ccount=DB::select("select count(id) as count from pt_user_info_comment where type=? and pt_type=1 and video_id='{$v['number_id']}'",[0]);
                $data[$k]['allCount']=isset($Ccount[0]['count']) ? $Ccount[0]['count'] : 0;
                //分享数
                if ($v['info_fans'] >= 10000) {
                    $num_fans .= "<span style='color:red;font-size:18px;'>" . round($v['info_fans'] / 10000, 1) . " W</span> 次分享";
                } else {
                    $num_fans .= "<span style='color:red;font-size:15px;'>" . $v['info_fans'] . "</span> 次分享";
                }
                $data[$k]['num_fans'] = $num_fans;
                $title="视频标题：".$v['info_signature'];
                $data[$k]['operation']="<a href=\"javascript:;\" title=\"查看评论\"><span class=\"glyphicon glyphicon-eye-open\" onclick=\"showContent('{$v['number_id']}','{$title}')\"></span></a>";
            }
        }
        return ['data' => $data, 'total' => $count];
    }
    //拉取用户数据
    //拉取数据20条	type	0视频，否则主页
    function getVideoComment($video_id,$type,$page=1,$hour,$day,$week){
        $size = 20;
        $offset = $size * ($page - 1);
        if(!$type){
            $countsql="select count(id) as count from pt_user_info_comment where type=0  and video_id=?";
            $sql="select * from pt_user_info_comment where type=0  and video_id=? ";
            if($hour==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1 hours' ));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($day==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1day'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($week==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1week'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }
           // print_r(2222);exit();
            $count=DB::select($countsql,[$video_id]);
            $count=isset($count[0]['count']) ? $count[0]['count'] : 0;
            $sql.=" order by create_time desc  limit {$offset},{$size}";
            $re=DB::select($sql,[$video_id]);
            $re=isset($re[0]['id']) ? $re : false;
        }else{
            $countsql="select count(id) as count from pt_user_info_comment where type=?";
            $sql="select * from pt_user_info_comment where type=?";
            if($hour==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1 hours' ));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($day==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1day'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($week==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1week'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }
            $sql.=" order by create_time desc limit {$offset},{$size}";
           // print_r(1111);exit();
            $count=DB::select($countsql,[$type]);
            $count=isset($count[0]['count']) ? $count[0]['count'] : 0;
            $re=DB::select($sql,[$type]);
            $re=isset($re[0]['id']) ? $re : false;
        }

        $html="";
        $nextPage=$page < ceil($count/$size) ? $page+1 : 0;
        if($re){
            foreach($re as $k=>$v){
//                $sj = date('Y-m-d',$v['update_time']);
                $html.="<tr>";
                $html.="<td><a href=\"https://www.douyin.com/user/{$v['sec_uid']}\" target=\"_blank\"><img src=\"{$v['userImg']}\" width=\"50px\" height=\"50px\"></a></td>";
                $html.="<td style=\"white-space:normal;min-width:150px;word-wrap:break-word;word-break:break-all;\">{$v['content']}</td>";
                $html.="<td>{$v['nickname']}</td>";
                $html.="<td>{$v['user_id']}<br/>".str_replace(["\r\n","\n","\r"],'<br/>',$v['tells'])."</td>";
                $html.="<td><a href=\"{$v['video_url']}\" target=\"_blank\">".mb_substr($v['video_url'],0,20)."...".mb_substr($v['video_url'],-10)."</a></td>";
                $html.="<td>".mb_substr($v['create_time'],0,22)."</td>";
                $html.="<td>".mb_substr($v['update_time'],0,10)."</td>";
                $statusText=$v['status']==0 ? "default" : "checkbox";
                $checkedText=$v['status']==0 ? "" : "checked";
                $html.="<td><div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"{$statusText}\" checked=\"{$checkedText}\" onclick=\"upStatus({$v['id']},this)\"><span></span></label></div></td>";
//                $follow="snssdk1128://user/profile/".$v['uid'];
//                $html.="<td><a href=\"javascript:void(0);\" title=\"扫码私聊\"><span class=\"btn btn-info\" onclick=\"following('{$follow}')\">扫码私聊</span></a></td>";
                $html.="</tr>";
            }
        }
        return ["html"=>$html,'nextPage'=>$nextPage];
    }



    //插入、更新用户监控数据
    function insertUser($data, $values)
    {
        $select_sql = "select id from pt_user_info where info_type = ? and number_id = ? and system_type = ? and partner_id = ? limit 1";
        $re = DB::select($select_sql, $values);
        $re = isset($re[0]) ? $re[0]['id'] : false;

        $sqlAll = "update pt_user_info set ";
        foreach ($data as $k => $v) {
            $sqlAll .= "`{$k}`='{$v}',";
        }
        //更新该 number_id 的所有数据
        $sqlAll = substr($sqlAll, 0, -1) . " where number_id='{$values[1]}'";
        DB::update($sqlAll);

        if (!$re) {
            $data['info_type'] = $values[0];
            $data['number_id'] = $values[1];
            $data['system_type'] = $values[2];
            $data['partner_id'] = $values[3];
            DB::table($this->table)->insertGetId($data);
        }
        return true;
    }

    //视频监控的评论入库
    function saveVideoComment($data, $video_id, $comment_id)
    {
        $values = [$video_id, $comment_id];
        $sql = "select id from pt_user_info_comment where video_id = ? and comment_id = ?";
        $re = DB::select($sql, $values);
        $id = isset($re[0]) ? $re[0]['id'] : '';
        if ($id) {
            $sql = "update pt_user_info_comment set ";
            foreach ($data as $k => $v) {
                $sql .= "`{$k}`='{$v}',";
            }
            $sql = substr($sql, 0, -1) . " where id={$id}";
            DB::update($sql);
        } else {
            $data['video_id'] = $video_id;
            $data['comment_id'] = $comment_id;
            DB::table("pt_user_info_comment")->insertGetId($data);
        }
    }

    //查询需要抓取评论的作品
    function getVideoInfo()
    {
        $sql = "select number_id,info_signature from pt_user_info where info_type=? and info_date>='" . date('Y-m-d H:i:s', time() - 300) . "' group by number_id";
        $re = DB::select($sql, [2]);
        return $re = isset($re[0]['number_id']) ? $re : false;
    }


}